package com.zyh.em.update;

import com.zyh.em.entity.HistoryData;
import com.zyh.em.entity.LoginSuccessData;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class HabitUpdate  extends AbstractUpdate{
    @Override
    public void update(LoginSuccessData loginSuccessData, HistoryData historyData, UpdateChain updateChain) {
        //
        long time = loginSuccessData.getTime();
        //从time中获取到星期以及小时；通过Calender完成的
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(time);
        //星期
        String week = instance.get(Calendar.DAY_OF_WEEK)+"";
        //小时
        String hour = instance.get(Calendar.HOUR_OF_DAY)+"";

        Map<String, Map<String, Integer>> map = historyData.getHabits();
        if(map==null){
            map=new HashMap<>();
            historyData.setHabits(map);
        }
        if(map.containsKey(week)){
            Map<String, Integer> xiaoMap = map.get(week);
            if(xiaoMap.containsKey(hour)){
                xiaoMap.put(hour,xiaoMap.get(hour)+1);
            }else{
                xiaoMap.put(hour,1);
            }
        }else{
            Map<String, Integer> xiaoMap=new HashMap<>();
            xiaoMap.put(hour,1);
            map.put(week,xiaoMap);
        }

        //
        updateChain.doUpdate(loginSuccessData, historyData);
    }
}